package com.powernode.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.Duration;

/**
 * @Description: redis缓存配置类
 * @Author: Tong Ziyu
 * @Date: 2024/10/28 15:46
 */
@Configuration
public class RedisCacheConfig {
    /**
     * 序列化相关配置
     *
     * @return
     */
    @Bean
    public RedisCacheConfiguration redisCacheConfiguration() {
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();

        // 进行redis配置
        redisCacheConfiguration
                // 将redis中值的序列化改成json
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()))
                // 统一设置redis中值的过期时间为7天
                .entryTtl(Duration.ofDays(7))
                // 禁止放空值
                .disableCachingNullValues();

        return redisCacheConfiguration;
    }

}
